const app = getApp()
const http = require('../../../../utils/util')
const Base64 = require('../../../../utils/base')
Page({

    /**
     * 页面的初始数据
     */
    data: {
		type:0,   //选项卡选中项
		orderList:[],
		status:0,  //状态
		page: 1,  //分页
		isnext:true, //是否有下一页
		paymentClass:'none',
		pay_type:1, //支付方式
		current_order:'', //选中支付的订单
    },

    getList: function () {
        let data = {
            user_id: wx.getStorageSync('user_id'),
            user_token: wx.getStorageSync('user_token'),
            status: this.data.status,
            type: 2,
            page: this.data.page
        }
        http.post('user/appoint',Base64.encode(JSON.stringify(data))).then(res => {
			//判断是否有下一页
			if(res.result.length==0){ 
				this.setData({
					isnext:false
				})
				return
			}
			if(res.status==1){
				this.setData({
					orderList:[...this.data.orderList,...res.result]
				})
			}else{
				wx.showToast({
					title:'获取数据失败'
				})
			}
        })
    },
	//选择支付方式
	changePayMethods(e){
		this.setData({
			pay_type:e.detail.value
		})
	},
	//显示支付方式
	showPayMethods(e){
		this.setData({
			paymentClass:'show',
			current_order:e.currentTarget.dataset.order
		})
	},
	// 关闭支付方式
	hidePayMethods(){
		this.setData({
			paymentClass:'hide'
		})
		setTimeout(()=>{
			this.setData({
				paymentClass:'none'
			})
		},200)
	},
	//支付
	payments(){
		let _this=this
		// 准备参数
		let data={
			user_id:wx.getStorageSync('user_id'),
			user_token:wx.getStorageSync('user_token'),
			room_id:this.data.current_order.id,
			type:this.data.pay_type
		}
		//支付逻辑
		http.post('user/updateRoom',Base64.encode(JSON.stringify(data))).then((res)=>{
			console.log(res);
			if(res.status==1){
				if(this.data.pay_type==1){ //微信支付
					let data=res.result
					wx.requestPayment({
					  timeStamp: data.timeStamp,
					  nonceStr: data.nonceStr,
					  package: data.package,
					  signType: data.signType,
					  paySign: data.paySign,
					  success:(res)=>{
						  _this.setData({
						  	paymentClass:'none',
						  	page:1,
						  	isnext:true,
							orderList: []
						  },()=>{
							  _this.getList()
						  })
						  
					  },
					  fail:()=>{
						  wx.showToast({
							  title:'支付失败',
							  icon:'none'
						  })
					  }
					})
				}else{ //储蓄卡支付
					wx.showToast({
						title:'支付成功',
						icon:'none'
					})
					
					// 刷新订单 关闭弹出层
					_this.setData({
						paymentClass:'none',
						page:1,
						isnext:true,
						orderList: []
					},()=>{
						_this.getList()
					})
					
				}
			}else{
				wx.showToast({
					title:res.message,
					icon:'none'
				})
			}
		})
	},
	//申请退款
	refund(e){
		let _this=this
		this.setData({
			current_order:e.currentTarget.dataset.order
		})
		wx.showModal({
		  title: '提示',
		  content: '确定要进行退款操作吗？',
		  success (res) {
		    if (res.confirm) {
		      // 用户进行退款
			  _this.rebate()
		    }
		  }
		})
	},
	//退款接口
	rebate(){
		let _this=this
		let data={
			user_id:wx.getStorageSync('user_id'),
			user_token:wx.getStorageSync('user_token'),
			type:1,
			order_id:this.data.current_order.id
		}
		http.post('user/orderRefund',Base64.encode(JSON.stringify(data))).then(res=>{
			if(res.status==1){
				wx.showToast({title:'退款成功',icon:'none'})
				//重置页面
				this.setData({
					page:1,
					isnext:true,
					orderList: []
				},()=>{
				   _this.getList()
				})
			}else{
				wx.showToast({
					title:res.message,
					icon:'none'
				})
			}
		})
	},
	
	
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		if(options.is_from){
			this.setData({
				is_from:true
			})
		}
        this.getList()
    },
	// 切换订单状态
    tabBar: function (e) {
		let status=e.currentTarget.dataset.type
        this.setData({
            type: e.currentTarget.dataset.type,
			status:status,
			isnext:true,
			page:1,
			orderList:[]
        })
        this.getList()
    },
	// 去往详情页
    orderDetail:function (e) {
        let id=e.currentTarget.dataset.item.id
		let params={
			order_id:id,
			type:2
		}
        wx.navigateTo({ 
            url: '/pages/mine/record/recordorderdetails/recordorderdetails?params='+JSON.stringify(params)
        })
    },
	//页面上拉加载
	onReachBottom(){
		if(this.data.isnext){
			this.setData({
				page:++this.data.page
			})
			this.getList()
		}
	},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
	onUnload(){
		if(getApp().globalData.is_from){
			wx.navigateTo({
				url:'/pages/mine/record/record'
			})
		}
	},
})